package com.cloudm.technician.ui.modules.personal.personal_center

import com.cloudm.technician.data.db.DbHelper
import com.cloudm.technician.data.model.UploadTokenInfo
import com.cloudm.technician.data.network.CommonApi
import com.cloudm.technician.data.network.PersonalApi
import com.cloudm.technician.data.preferences.PreferencesHelper
import com.cloudm.technician.extensions.buildUploadTokenKey
import com.cloudm.technician.ui.base.RxPresenter
import com.qiniu.android.storage.UpCompletionHandler
import com.qiniu.android.storage.UploadManager
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.io.File

/**
 * 作者: wesley
 * 时间: 2017/12/26
 * 描述: 业务逻辑
 */
class PersonalCenterPresenter(private val view: PersonalCenterContract.View,
                              private val personalApi: PersonalApi,
                              private val commonApi: CommonApi,
                              private val uploadManager: UploadManager,
                              private val preferencesHelper: PreferencesHelper,
                              private val dbHelper: DbHelper) : RxPresenter(),
        PersonalCenterContract.Presenter {


    override fun getUserInfo() {
        view.userInfoLoadSuccess(dbHelper.queryUserInfoByToken(preferencesHelper.token))
    }

    override fun exitLogin() {
        preferencesHelper.isLogin = false
        view.exitLogin()
    }


    override fun getUploadToken(file: File) {
        request(commonApi.getUploadInfo(), view, onSuccess = { info, _ ->
            when (info) {
                null -> {
                    view.showTokenInfoEmpMsg()
                    view.updateUiByFlag(0)
                }
                else -> {
                    view.loadTokenInfoSuccess(info, file)
                }
            }
        }, onFailure = { errorMsg ->
            view.showErrorMsg(errorMsg)
            view.updateUiByFlag(0)
        })
    }

    override fun uploadFile(info: UploadTokenInfo, file: File) {
        Observable.create(ObservableOnSubscribe<String> { e ->
            uploadManager.put(file, buildUploadTokenKey(), info.uptoken,
                    UpCompletionHandler { key, responseInfo, _ ->
                        if (responseInfo == null || !responseInfo.isOK) {
                            e.onNext("")
                            return@UpCompletionHandler
                        }
                        e.onNext(info.origin + key)
                    }, null)
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe { url ->
            if (url == null || url.isNullOrEmpty()) {
                view.picUploadFailure()
                view.updateUiByFlag(0)
            } else {
                view.picUploadSuccess(url)
            }
        }
    }

    override fun uploadLogo(uploadLogo: String) {
        request(personalApi.modifyLogo(uploadLogo), view, onSuccess = { _, _ ->
            view.modifyLogoSuccess(uploadLogo)
            dbHelper.updateUserLogo(dbHelper.queryUserInfoByToken(preferencesHelper.token),
                    uploadLogo)
        }, onFailure = { errorMsg ->
            view.showErrorMsg(errorMsg)
            view.updateUiByFlag(0)
        })
    }

}